
package com.htj.erp.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.htj.erp.dao.ProductDao;
import com.htj.erp.dao.mybatis.ProductMapper;
import com.htj.erp.exception.BusinessException;
import com.htj.erp.model.jpa.Product;
import com.htj.erp.model.mybatis.ProductPO;
import com.htj.erp.vo.ProductVO;
import com.kedacom.security.service.GenericCpsService;
/**
 * 
 *
 * 
 * ProductService
 * product service 处理类
 * 这个基本类一般继承com.kedacom.security.service.GenericCpsService
 * 如果自己实现转换类，通过重写getXXConveter方法。
 * chenghongtao
 * 2016-11-04 23:25:15
 * HTJ
 * @version 1.0.0
 *
 */
@Service
public class ProductService extends GenericCpsService<Product,ProductPO,ProductVO,Long>  {

    
	@Resource
	private ProductDao productDao;
	
	@Resource
	private ProductMapper productMapper;
	
	/**
	 * 获取jpa的dao
	 */
	@Override
	protected  ProductDao getEntityJpaDao ()
	{
		return this.productDao;
	}
	 
	  
	/**
	 * 获取 mubatis 的dao
	 */
	@Override
	protected  ProductMapper getEntityDao ()
	{
		return this.productMapper;
	}
	
	
	
	/**
	 * 
	 * saveOrUpdate 保存或者更新
	 * @param productVO
	 * @return
	 *ProductVO
	 * @exception
	 * @since  1.0.0
	 */
	public ProductVO saveOrUpdate(ProductVO productVO) throws Exception
	{
	    if(productVO.getId() == null || productVO.getId() == 0L)
	    {
	        return this.add(productVO);
	    }
	    else
	    {
	        return this.update(productVO);
	    }
	    
	}
	
	public Map listProductSumInfo()
	{
		Map map = new HashMap();
		List<ProductPO> list =  this.getEntityDao().listProductSumInfo();
		List<ProductPO> listFooter = new ArrayList<ProductPO>();
		ProductPO po1  = new ProductPO();
		po1.setName("汇总：");
		Double d = 0d;
		for(ProductPO po : list)
		{
			Double t = new BigDecimal(po.getPprice()).multiply(new BigDecimal(po.getRestNum())).setScale(2, RoundingMode.HALF_UP).doubleValue();
			po.setTotalPrice(t);
			d += t;
		}
		po1.setTotalPrice(d);
		map.put("rows", list);
		listFooter.add(po1);
		map.put("footer", listFooter);
		return map;
	}


	@Override
	public void delById(Long id) throws Exception {
		Integer num = this.getEntityDao().getProductUsed(id);
		if(num > 0)
			throw new BusinessException("产品已经被使用无法删除");
		super.delById(id);
	}


	@Override
	public void delByIds(Long[] ids) throws Exception {
		for(Long id : ids)
			delById(id);
	}
	
	
	
	
}
